// 路由配置
import Vue from 'vue'
import VueRouter from 'vue-router'
import store from '@/store/index'

const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location) {
    return originalPush.call(this, location, () => { }, () => { })
}

const originalReplace = VueRouter.prototype.replace
VueRouter.prototype.replace = function replace(location) {
    return originalReplace.call(this, location, () => { }, () => { })
}

Vue.use(VueRouter) // 正确地进行全局注册

const router = new VueRouter({
    mode: 'history',
    routes: [
        {
            path: '/',
            name: 'home',
            redirect: '/main'
        },
        {
            path: '/login',
            name: 'login',
            component: () => import('@/views/login/index')
        },
        {
            path: '/main',
            name: 'main',
            component: () => import('@/views/main/index'),
            redirect: '/main/dashboard',
            children: [
                {
                    path: 'dashboard',
                    name: 'dashboard',
                    component: () => import('@/views/main/components/dashboard/index'),
                },
                {
                    path: 'orders',
                    name: 'orders',
                    component: () => import('@/views/main/components/orders/index'),
                },
                {
                    path: 'product-list',
                    name: 'product-list',
                    component: () => import('@/views/main/components/product-list/index'),
                },
                {
                    path: 'operation-data',
                    name: 'operation-data',
                    component: () => import('@/views/main/components/operation-data/index')
                },
                {
                    path: 'promotion',
                    name: 'promotion',
                    component: () => import('@/views/main/components/promotion/index')
                },
                {
                    path: 'ordered-app',
                    name: 'ordered-app',
                    component: () => import('@/views/main/components/ordered-app/index')
                },
                {
                    path: 'my-shop',
                    name: 'my-shop',
                    component: () => import('@/views/main/components/my-shop/index')
                },
                {
                    path: 'discount',
                    name: 'discount',
                    component: () => import('@/views/main/components/discount/index')
                },
                {
                    path: 'setting',
                    name: 'setting',
                    component: () => import('@/views/main/components/setting/index')
                },
                {
                    path: 'help',
                    name: 'help',
                    component: () => import('@/views/main/components/help/index')
                }
            ]
        }
    ]
})

router.beforeEach((to, from, next) => {
    if (!store.state.login.userInfo.token && to.name !== 'login') {
        next({ path: 'login' })
    } else {
        next()
    }
})

export default router